
# For more information about using CMake with Android Studio, read the
# documentation: https://d.android.com/studio/projects/add-native-code.html

# Sets the minimum version of CMake required to build the native library.

PROJECT(VideoAgent)
cmake_minimum_required(VERSION 3.4.1)

set(CMAKE_VIDEOAGENT_NAME VideoAgent)
# Creates and names a library, sets it as either STATIC
# or SHARED, and provides the relative paths to its source code.
# You can define multiple libraries, and CMake builds them for you.
# Gradle automatically packages shared libraries with your APK.

# Searches for a specified prebuilt library and stores the path as a
# variable. Because CMake includes system libraries in the search path by
# default, you only need to specify the name of the public NDK library
# you want to add. CMake verifies that the library exists before
# completing its build.

#add_definitions(-DEMUGL_DEBUG)
#add_definitions(-DCMAKE_BUILD_TYPE=Debug)
add_definitions(-DANDROID)
#add_definitions(-DCHECK_GL_ERROR)#use this with DEBUG_PRINTOUT on
#add_definitions(-DDEBUG_HANDLE_MAP)
#add_definitions(-DNO_BLIT)
add_definitions(-DDECOMPRESS_DEBUG=1)
add_definitions(-DSAVE_DECOMPRESSED_STREAM=0)
add_definitions(-DREVERSE_OPCODE_IN_DECODER=1)
#add_definitions(-D_LAYER_DEBUG)
#add_definitions(-D_LAYER_TEXTURE_DEBUG)
add_definitions(-DSQLITE_SYNC_PERIOD=20)

add_definitions(-DRECONNECT_GAME)
#use Fillp lib
add_definitions(-DENABLE_STATISTICS=0)
#add_definitions(-Werror=return-type)
add_definitions(-Werror)

add_definitions(-D__OPTIMIZE__) # for opus

set(NORMAL_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -fPIC -fexceptions -Wno-error -fprofile-arcs -ftest-coverage -fpermissive -rdynamic" )
set(CMAKE_SKIP_RPATH TRUE)
set(CMAKE_CXX_FLAGS "${NORMAL_CXX_FLAGS}")

#set(CMAKE_VERBOSE_MAKEFILE ON)

SET(LIBS_COMMON_REL_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../../../Common)
SET(THIRDPARTY_REL_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../../../ThirdParty)
SET(VIDEO_REL_DIR ${CMAKE_CURRENT_SOURCE_DIR}/..)
SET(CLIENT_REL_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../../../ArmNative/Client/)

get_filename_component(LIBS_COMMON_DIR ${LIBS_COMMON_REL_DIR} ABSOLUTE)
get_filename_component(THIRDPARTY_DIR ${THIRDPARTY_REL_DIR} ABSOLUTE)
get_filename_component(VIDEO_DIR ${VIDEO_REL_DIR} ABSOLUTE)
get_filename_component(CLIENT_DIR ${CLIENT_REL_DIR} ABSOLUTE)

include_directories(${THIRDPARTY_DIR}/)
include_directories(${LIBS_COMMON_DIR}/Communication/)
include_directories(${LIBS_COMMON_DIR}/)
include_directories(${VIDEO_DIR}/)
include_directories(${CLIENT_DIR}/AndroidApp/SDK/app/src/main/cpp)
include_directories(/usr/local/include/)

include(${CMAKE_ROOT}/Modules/ExternalProject.cmake)


find_library( android-lib android)
find_library( egl-lib EGL)
find_library(cpp-lib NAMES libc++_static.a)


message(STATUS ${PROJECT_BINARY_DIR})
if(NOT TARGET VmiHWSecure)
add_subdirectory(${THIRDPARTY_DIR}/HWSecure ${PROJECT_BINARY_DIR}/ThirdParty/HWSecure/)
endif()
if(NOT TARGET VmiCommon)
add_subdirectory(${LIBS_COMMON_DIR} ${PROJECT_BINARY_DIR}/Common)
endif()

add_subdirectory(${THIRDPARTY_DIR}/cjson/json ${PROJECT_BINARY_DIR}/ThirdParty/cJSON/)

include_directories(${CMAKE_CURRENT_SOURCE_DIR})

function(build_open_source arg)
    ExternalProject_Add(
        ${ARGV0}
        SOURCE_DIR    ${CMAKE_SOURCE_DIR}/../../../open_source/${ARGV0}
        BINARY_DIR ${CMAKE_BINARY_DIR}/${ARGV0}
        INSTALL_COMMAND ""
      )
endfunction()

foreach(open_source IN LISTS open_sources)
        build_open_source(${open_source})
endforeach(open_source IN LISTS open_sources)


LINK_DIRECTORIES(/usr/local/lib/)

add_definitions(-DLOG_TAG="VideoAgent")
add_executable(${PROJECT_NAME}
EncoderPoolClient/RrConfig.cpp
EncoderPoolClient/socket_client_common.cpp
main.cpp
VideoAgent.cpp
VideoEncoder.cpp
VideoSocketAgent.cpp
RemoteVideoEncoder.cpp
VideoEngine.cpp)

add_library(videoagent
EncoderPoolClient/RrConfig.cpp
EncoderPoolClient/socket_client_common.cpp
VideoAgent.cpp
VideoEncoder.cpp
VideoSocketAgent.cpp
RemoteVideoEncoder.cpp
VideoEngine.cpp)

target_link_libraries(${PROJECT_NAME}
avcodec
avutil
pthread
VmiCommon
cjson
)
